package com.bnz.service.impl;

import com.bnz.mapper.ClassesMapper;
import com.bnz.mapper.StudentMapper;
import com.bnz.pojo.PageResult;
import com.bnz.pojo.Student;
import com.bnz.service.StudentService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private ClassesMapper classesMapper;
    // 1. 查询所有学生
    @Override
    public List<Student> findAll() {
        // 1.1 查询所有学生
        List<Student> students = studentMapper.selectAll();
        // 1.2 循环找到每个学生的班级
        for (Student student : students) {
            student.setCname(classesMapper.selectByPrimaryKey(student.getCid()).getCname());
        }
        // 1.3 返回学生列表
        return students;
    }

    // 2. 分页查询
    @Override
    public PageResult<Student> findByPage(int page, int pageSize) {
        // 2.1 开始分页
        PageHelper.startPage(page,pageSize);
        // 2.2 查询所有学生
        Example example = new Example(Student.class);
        List<Student> students = studentMapper.selectByExample(example);
        // 2.3 给每个学生找到对应的班级
        for (Student student : students) {
            student.setCname(classesMapper.selectByPrimaryKey(student.getCid()).getCname());
        }
        // 2.4 将学生集合转换为Page<Student>集合
        Page<Student> studentPage = (Page<Student>) students;
        // 2.5 返回
        return new PageResult<>(studentPage.getTotal(),studentPage.getPages(),studentPage.getResult(),page);
    }

    // 3. 条件查询带分页
    @Override
    public PageResult<Student> search(int page, int pageSize, Student student) {
        // 3.1 开始分页
        PageHelper.startPage(page,pageSize);
        // 3.2 查询所有学生，添加封装查询条件
        Example example = new Example(Student.class);
        Example.Criteria criteria = example.createCriteria();
        // 3.3 先进行判断，添加封装条件
        if (student != null){
            if (StringUtils.isNotBlank(student.getSname())){
                criteria.andLike("sname","%" + student.getSname() + "%");
            }
            if (StringUtils.isNotBlank(student.getAddr())){
                criteria.andLike("addr","%" + student.getAddr() + "%");
            }
            if (student.getCid() != null && student.getCid()!= 0){
                criteria.andEqualTo("cid",student.getCid());
            }
        }
        List<Student> students = studentMapper.selectByExample(example);
        // 3.3 给每个学生找到对应的班级
        for (Student student1 : students) {
            student1.setCname(classesMapper.selectByPrimaryKey(student1.getCid()).getCname());
        }
        // 3.4 将学生集合转换为Page<Student>集合
        Page<Student> studentPage = (Page<Student>) students;
        // 3.5 返回
        return new PageResult<>(studentPage.getTotal(),studentPage.getPages(),studentPage.getResult(),page);
    }

    // 4. 根据学生id查询对应学生
    @Override
    public Student findById(String sid) {
        // 根据学生id查询学生
        return studentMapper.selectByPrimaryKey(sid);
    }

    // 5. 添加学生
    @Override
    public void add(Student student) {
        // 添加学生
        studentMapper.insert(student);
    }

    // 6. 修改学生
    @Override
    public void update(Student student) {
        studentMapper.updateByPrimaryKey(student);
    }

    // 7. 删除学生
    @Override
    public void delete(String sid) {
        studentMapper.deleteByPrimaryKey(sid);
    }


}
